using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace etbTOEIC
{
	/// <summary>
	/// Summary description for FormEditBook.
	/// </summary>
	public class FormEditBook : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListView lvBooks;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnDel;
		private etbTOEIC.UCBookInfo ucBookInfo1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormEditBook()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormEditBook));
			this.lvBooks = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.button1 = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnDel = new System.Windows.Forms.Button();
			this.ucBookInfo1 = new etbTOEIC.UCBookInfo();
			this.SuspendLayout();
			// 
			// lvBooks
			// 
			this.lvBooks.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					  this.columnHeader1});
			this.lvBooks.Location = new System.Drawing.Point(320, 40);
			this.lvBooks.Name = "lvBooks";
			this.lvBooks.Size = new System.Drawing.Size(184, 176);
			this.lvBooks.TabIndex = 5;
			this.lvBooks.View = System.Windows.Forms.View.List;
			this.lvBooks.SelectedIndexChanged += new System.EventHandler(this.lvBooks_SelectedIndexChanged);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Book Name";
			this.columnHeader1.Width = 200;
			// 
			// groupBox1
			// 
			this.groupBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("groupBox1.BackgroundImage")));
			this.groupBox1.Location = new System.Drawing.Point(304, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(216, 224);
			this.groupBox1.TabIndex = 6;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Book List";
			// 
			// button1
			// 
			this.button1.BackColor = System.Drawing.SystemColors.ActiveCaption;
			this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
			this.button1.Location = new System.Drawing.Point(200, 184);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(96, 32);
			this.button1.TabIndex = 12;
			this.button1.Text = "&Close";
			this.button1.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnSave
			// 
			this.btnSave.BackColor = System.Drawing.SystemColors.ActiveCaption;
			this.btnSave.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnSave.BackgroundImage")));
			this.btnSave.Location = new System.Drawing.Point(8, 184);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(96, 32);
			this.btnSave.TabIndex = 11;
			this.btnSave.Text = "&Update";
			this.btnSave.Click += new System.EventHandler(this.hoverGradientButton1_Click);
			// 
			// btnDel
			// 
			this.btnDel.BackColor = System.Drawing.SystemColors.ActiveCaption;
			this.btnDel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnDel.BackgroundImage")));
			this.btnDel.Location = new System.Drawing.Point(104, 184);
			this.btnDel.Name = "btnDel";
			this.btnDel.Size = new System.Drawing.Size(96, 32);
			this.btnDel.TabIndex = 11;
			this.btnDel.Text = "&Delete";
			this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
			// 
			// ucBookInfo1
			// 
			this.ucBookInfo1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ucBookInfo1.BackgroundImage")));
			this.ucBookInfo1.BookCode = "";
			this.ucBookInfo1.BookName = "";
			this.ucBookInfo1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.ucBookInfo1.Location = new System.Drawing.Point(0, 16);
			this.ucBookInfo1.Name = "ucBookInfo1";
			this.ucBookInfo1.Size = new System.Drawing.Size(296, 136);
			this.ucBookInfo1.TabIndex = 13;
			this.ucBookInfo1.Writer = "";
			// 
			// FormEditBook
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = new System.Drawing.Size(536, 238);
			this.ControlBox = false;
			this.Controls.Add(this.ucBookInfo1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.lvBooks);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.btnDel);
			this.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "FormEditBook";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Edit Book Infomations";
			this.Load += new System.EventHandler(this.FormEditBook_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void lvBooks_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(lvBooks.SelectedItems.Count > 0)
			{
				int nID = Convert.ToInt32(lvBooks.SelectedItems[0].Tag.ToString());
				DB2MainForm.LoadBook(nID, ucBookInfo1);
			}
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnDel_Click(object sender, System.EventArgs e)
		{
			if(lvBooks.SelectedItems.Count < 1)
				return;
			int nID = Convert.ToInt32(lvBooks.SelectedItems[0].Tag.ToString());
			if(MyDatabase.DeleteBook(nID))
			{
				MessageBox.Show("Book deleted", "Delete Book");
				lvBooks.Items.Clear();
				DB2MainForm.LoadBooks(lvBooks);
			}
			else
				MessageBox.Show("Cannot delete book.", "Delete Book - Error");
		}

		private void hoverGradientButton1_Click(object sender, System.EventArgs e)
		{
			if(DB2MainForm.UpdateBook(ucBookInfo1))
				MessageBox.Show("Update Succesfully.","Update Book Info");
			else
				MessageBox.Show("Error. Can not update book info.","Update Book Info - Error");
		}

		private void FormEditBook_Load(object sender, System.EventArgs e)
		{
			DB2MainForm.LoadBooks(lvBooks);
		}
	}
}
